""" Options:
Date: 2025-10-14 12:46:39
Version: 8.81
Tip: To override a DTO option, remove "#" prefix before updating
BaseUrl: http://localhost:5000

#GlobalNamespace: 
#AddServiceStackTypes: True
#AddResponseStatus: False
#AddImplicitVersion: 
#AddDescriptionAsComments: True
IncludeTypes: ChatCompletion.*
#ExcludeTypes: 
#DefaultImports: datetime,decimal,marshmallow.fields:*,servicestack:*,typing:*,dataclasses:dataclass/field,dataclasses_json:dataclass_json/LetterCase/Undefined/config,enum:Enum/IntEnum
#DataClass: 
#DataClassJson: 
"""

import datetime
import decimal
from marshmallow.fields import *
from servicestack import *
from typing import *
from dataclasses import dataclass, field
from dataclasses_json import dataclass_json, LetterCase, Undefined, config
from enum import Enum, IntEnum
Object = TypeVar('Object')


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiContent:
    type: Optional[str] = None
    """
    The type of the content part.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class ToolCall:
    """
    The tool calls generated by the model, such as function calls.
    """

    id: Optional[str] = None
    """
    The ID of the tool call.
    """

    type: Optional[str] = None
    """
    The type of the tool. Currently, only `function` is supported.
    """

    function: Optional[str] = None
    """
    The function that the model called.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiMessage:
    """
    A list of messages comprising the conversation so far.
    """

    content: Optional[List[AiContent]] = None
    """
    The contents of the message.
    """

    role: Optional[str] = None
    """
    The role of the author of this message. Valid values are `system`, `user`, `assistant` and `tool`.
    """

    name: Optional[str] = None
    """
    An optional name for the participant. Provides the model information to differentiate between participants of the same role.
    """

    tool_calls: Optional[List[ToolCall]] = None
    """
    The tool calls generated by the model, such as function calls.
    """

    tool_call_id: Optional[str] = None
    """
    Tool call that this message is responding to.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiChatAudio:
    """
    Parameters for audio output. Required when audio output is requested with modalities: [audio]
    """

    format: Optional[str] = None
    """
    Specifies the output audio format. Must be one of wav, mp3, flac, opus, or pcm16.
    """

    voice: Optional[str] = None
    """
    The voice the model uses to respond. Supported voices are alloy, ash, ballad, coral, echo, fable, nova, onyx, sage, and shimmer.
    """


class ResponseFormat(str, Enum):
    TEXT = 'text'
    JSON_OBJECT = 'json_object'


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiResponseFormat:
    type: Optional[ResponseFormat] = None
    """
    An object specifying the format that the model must output. Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106.
    """


class ToolType(str, Enum):
    FUNCTION = 'function'


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class Tool:
    type: Optional[ToolType] = None
    """
    The type of the tool. Currently, only function is supported.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class UrlCitation:
    """
    Annotations for the message, when applicable, as when using the web search tool.
    """

    end_index: int = 0
    """
    The index of the last character of the URL citation in the message.
    """

    start_index: int = 0
    """
    The index of the first character of the URL citation in the message.
    """

    title: Optional[str] = None
    """
    The title of the web resource.
    """

    url: Optional[str] = None
    """
    The URL of the web resource.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class ChoiceAnnotation:
    """
    Annotations for the message, when applicable, as when using the web search tool.
    """

    type: Optional[str] = None
    """
    The type of the URL citation. Always url_citation.
    """

    url_citation: Optional[UrlCitation] = None
    """
    A URL citation when using web search.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class ChoiceAudio:
    """
    If the audio output modality is requested, this object contains data about the audio response from the model.
    """

    data: Optional[str] = None
    """
    Base64 encoded audio bytes generated by the model, in the format specified in the request.
    """

    expires_at: int = 0
    """
    The Unix timestamp (in seconds) for when this audio response will no longer be accessible on the server for use in multi-turn conversations.
    """

    id: Optional[str] = None
    """
    Unique identifier for this audio response.
    """

    transcript: Optional[str] = None
    """
    Transcript of the audio generated by the model.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class ChoiceMessage:
    content: Optional[str] = None
    """
    The contents of the message.
    """

    refusal: Optional[str] = None
    """
    The refusal message generated by the model.
    """

    reasoning: Optional[str] = None
    """
    The reasoning process used by the model.
    """

    role: Optional[str] = None
    """
    The role of the author of this message.
    """

    annotations: Optional[List[ChoiceAnnotation]] = None
    """
    Annotations for the message, when applicable, as when using the web search tool.
    """

    audio: Optional[ChoiceAudio] = None
    """
    If the audio output modality is requested, this object contains data about the audio response from the model.
    """

    tool_calls: Optional[List[ToolCall]] = None
    """
    The tool calls generated by the model, such as function calls.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class Choice:
    finish_reason: Optional[str] = None
    """
    The reason the model stopped generating tokens. This will be stop if the model hit a natural stop point or a provided stop sequence, length if the maximum number of tokens specified in the request was reached, content_filter if content was omitted due to a flag from our content filters, tool_calls if the model called a tool
    """

    index: int = 0
    """
    The index of the choice in the list of choices.
    """

    message: Optional[ChoiceMessage] = None
    """
    A chat completion message generated by the model.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiCompletionUsage:
    """
    Usage statistics for the completion request.
    """

    accepted_prediction_tokens: int = 0
    """
    When using Predicted Outputs, the number of tokens in the prediction that appeared in the completion.
    """

    audio_tokens: int = 0
    """
    Audio input tokens generated by the model.
    """

    reasoning_tokens: int = 0
    """
    Tokens generated by the model for reasoning.
    """

    rejected_prediction_tokens: int = 0
    """
    When using Predicted Outputs, the number of tokens in the prediction that did not appear in the completion.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiPromptUsage:
    """
    Breakdown of tokens used in the prompt.
    """

    accepted_prediction_tokens: int = 0
    """
    When using Predicted Outputs, the number of tokens in the prediction that appeared in the completion.
    """

    audio_tokens: int = 0
    """
    Audio input tokens present in the prompt.
    """

    cached_tokens: int = 0
    """
    Cached tokens present in the prompt.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiUsage:
    """
    Usage statistics for the completion request.
    """

    completion_tokens: int = 0
    """
    Number of tokens in the generated completion.
    """

    prompt_tokens: int = 0
    """
    Number of tokens in the prompt.
    """

    total_tokens: int = 0
    """
    Total number of tokens used in the request (prompt + completion).
    """

    completion_tokens_details: Optional[AiCompletionUsage] = None
    """
    Breakdown of tokens used in a completion.
    """

    prompt_tokens_details: Optional[AiPromptUsage] = None
    """
    Breakdown of tokens used in the prompt.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiTextContent(AiContent):
    """
    Text content part
    """

    text: Optional[str] = None
    """
    The text content.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiImageUrl:
    url: Optional[str] = None
    """
    Either a URL of the image or the base64 encoded image data.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiImageContent(AiContent):
    """
    Image content part
    """

    image_url: Optional[AiImageUrl] = None
    """
    The image for this content.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiInputAudio:
    """
    Audio content part
    """

    data: Optional[str] = None
    """
    URL or Base64 encoded audio data.
    """

    format: Optional[str] = None
    """
    The format of the encoded audio data. Currently supports 'wav' and 'mp3'.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiAudioContent(AiContent):
    """
    Audio content part
    """

    input_audio: Optional[AiInputAudio] = None
    """
    The audio input for this content.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiFile:
    """
    File content part
    """

    file_data: Optional[str] = None
    """
    The URL or base64 encoded file data, used when passing the file to the model as a string.
    """

    filename: Optional[str] = None
    """
    The name of the file, used when passing the file to the model as a string.
    """

    file_id: Optional[str] = None
    """
    The ID of an uploaded file to use as input.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class AiFileContent(AiContent):
    """
    File content part
    """

    file: Optional[AiFile] = None
    """
    The file input for this content.
    """


@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class ChatResponse:
    id: Optional[str] = None
    """
    A unique identifier for the chat completion.
    """

    choices: List[Choice] = field(default_factory=list)
    """
    A list of chat completion choices. Can be more than one if n is greater than 1.
    """

    created: int = 0
    """
    The Unix timestamp (in seconds) of when the chat completion was created.
    """

    model: Optional[str] = None
    """
    The model used for the chat completion.
    """

    system_fingerprint: Optional[str] = None
    """
    This fingerprint represents the backend configuration that the model runs with.
    """

    object: Optional[str] = None
    """
    The object type, which is always chat.completion.
    """

    service_tier: Optional[str] = None
    """
    Specifies the processing type used for serving the request.
    """

    usage: Optional[AiUsage] = None
    """
    Usage statistics for the completion request.
    """

    metadata: Optional[Dict[str, str]] = None
    """
    Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format.
    """

    response_status: Optional[ResponseStatus] = None


# @Route("/v1/chat/completions", "POST")
@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass
class ChatCompletion(IReturn[ChatResponse], IPost):
    """
    Chat Completions API (OpenAI-Compatible)
    """

    messages: List[AiMessage] = field(default_factory=list)
    """
    The messages to generate chat completions for.
    """

    model: Optional[str] = None
    """
    ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API
    """

    audio: Optional[AiChatAudio] = None
    """
    Parameters for audio output. Required when audio output is requested with modalities: [audio]
    """

    logit_bias: Optional[Dict[int, int]] = None
    """
    Modify the likelihood of specified tokens appearing in the completion.
    """

    metadata: Optional[Dict[str, str]] = None
    """
    Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format.
    """

    reasoning_effort: Optional[str] = None
    """
    Constrains effort on reasoning for reasoning models. Currently supported values are minimal, low, medium, and high (none, default). Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response.
    """

    response_format: Optional[AiResponseFormat] = None
    """
    An object specifying the format that the model must output. Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`. Setting Type to ResponseFormat.JsonObject enables JSON mode, which guarantees the message the model generates is valid JSON.
    """

    service_tier: Optional[str] = None
    """
    Specifies the processing type used for serving the request.
    """

    safety_identifier: Optional[str] = None
    """
    A stable identifier used to help detect users of your application that may be violating OpenAI's usage policies. The IDs should be a string that uniquely identifies each user.
    """

    stop: Optional[List[str]] = None
    """
    Up to 4 sequences where the API will stop generating further tokens.
    """

    modalities: Optional[List[str]] = None
    """
    Output types that you would like the model to generate. Most models are capable of generating text, which is the default:
    """

    prompt_cache_key: Optional[str] = None
    """
    Used by OpenAI to cache responses for similar requests to optimize your cache hit rates.
    """

    tools: Optional[List[Tool]] = None
    """
    A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported.
    """

    verbosity: Optional[str] = None
    """
    Constrains the verbosity of the model's response. Lower values will result in more concise responses, while higher values will result in more verbose responses. Currently supported values are low, medium, and high.
    """

    temperature: Optional[float] = None
    """
    What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
    """

    max_completion_tokens: Optional[int] = None
    """
    An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens.
    """

    top_logprobs: Optional[int] = None
    """
    An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to true if this parameter is used.
    """

    top_p: Optional[float] = None
    """
    An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
    """

    frequency_penalty: Optional[float] = None
    """
    Number between `-2.0` and `2.0`. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
    """

    presence_penalty: Optional[float] = None
    """
    Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
    """

    seed: Optional[int] = None
    """
    This feature is in Beta. If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result. Determinism is not guaranteed, and you should refer to the system_fingerprint response parameter to monitor changes in the backend.
    """

    n: Optional[int] = None
    """
    How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep `n` as `1` to minimize costs.
    """

    store: Optional[bool] = None
    """
    Whether or not to store the output of this chat completion request for use in our model distillation or evals products.
    """

    logprobs: Optional[bool] = None
    """
    Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content of message.
    """

    parallel_tool_calls: Optional[bool] = None
    """
    Whether to enable parallel function calling during tool use.
    """

    enable_thinking: Optional[bool] = None
    """
    Whether to enable thinking mode for some Qwen models and providers.
    """

    stream: Optional[bool] = None
    """
    If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a `data: [DONE]` message.
    """

